const koa = require('koa')
const Router = require('koa-router')
const mongoose = require('mongoose')
const dbConfig = require('./config/db')
const bodyparser = require('koa-bodyparser')
const passport = require('koa-passport')
const static = require('koa-static') // 存放静态资源包




const app = new koa()
const router = new Router()
const person = require('./routes/api/person')
const user = require('./routes/api/user')

app.use(bodyparser())
app.use(passport.initialize())
app.use(passport.session())
// app.use(static(
//     path.join(__dirname, staticPath)
// ))
require('./config/passprot')(passport)
// koa-static  需要定义是./相对定位的路径  然后在指定/static
app.use(static('./'));
app.use(static(__dirname + '/static'));
// koa-static end
// app.use(router.routes()).use(router.allowedMethods()) 
// router.use("/api/person", person)  这个问题没解决
app.use(person.routes(), person.allowedMethods()) // 这种写发必须是require引入的那个是router没有加routes()的方法
app.use(user.routes(), user.allowedMethods())

mongoose.connect(dbConfig.dbs, { useNewUrlParser: true }, (err, res) => {
    if (err) throw err
    console.log('mongodb connect success')
})

const port = process.env.PORT || 4550
app.listen(port, () => {
    console.log('4550 success')
})